home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLINC.PAK / GADGETWI.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  14KB  |  429 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1992, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.14  $
  6. //
  7. // Definition of TGadgetList, TGadgetWindow & TGadgetWindowFont
  8. // A list holding gadgets, & a window class owning & managing tiled gadgets.
  9. //----------------------------------------------------------------------------
  10. #if !defined(OWL_GADGETWI_H)
  11. #define OWL_GADGETWI_H
  12.  
  13. #if !defined(OWL_WINDOW_H)
  14. # include <owl/window.h>
  15. #endif
  16. #if !defined(OWL_GADGET_H)
  17. # include <owl/gadget.h>
  18. #endif
  19. #if !defined(OWL_GDIOBJEC_H)
  20. # include <owl/gdiobjec.h>
  21. #endif
  22.  
  23. #if defined(BI_NAMESPACE)
  24. namespace OWL {
  25. #endif
  26.  
  27. class _OWLCLASS TCelArray;
  28. class _OWLCLASS TTooltip;
  29.  
  30. // Generic definitions/compiler options (eg. alignment) preceeding the
  31. // definition of classes
  32. #include <services/preclass.h>
  33.  
  34. //
  35. // class TGadgetList
  36. // ~~~~~ ~~~~~~~~~~~
  37. // A list of Gadgets with management functions
  38. //
  39. class _OWLCLASS TGadgetList {
  40.   public:
  41.  
  42.     // Insert a Gadget or a list of gadgets. Gadgets are removed from
  43.     // the source list
  44.     //
  45.     enum TPlacement {Before, After};
  46.     virtual void  Insert(TGadget& gadget, TPlacement = After,
  47.                          TGadget* sibling = 0);
  48.     virtual void  InsertFrom(TGadgetList& list, TPlacement = After,
  49.                          TGadget* sibling = 0);
  50.  
  51.     // Removes (unlinks) a gadget.
  52.     //
  53.     virtual TGadget*  Remove(TGadget& gadget);
  54.  
  55.     // Callbacks invoked when a gadget is removed or inserted
  56.     //
  57.     virtual void  Inserted(TGadget& gadget);
  58.     virtual void  Removed(TGadget& gadget);
  59.  
  60.     // Insert/remove methods that take a source/dest TGadgetList&
  61.  
  62.     // Locate and iterate over gadgets
  63.     //
  64.     uint          GadgetCount() const;
  65.     TGadget*      FirstGadget() const;
  66.     TGadget*      NextGadget(TGadget& gadget) const;
  67.     TGadget*      GadgetFromPoint(TPoint& point) const;
  68.     TGadget*      GadgetWithId(int id) const;
  69.     TGadget*      operator [](uint index);
  70.  
  71.   protected_data:
  72.     TGadget*        Gadgets;        // Linked list of gadgets
  73.     uint            NumGadgets;     // Total number of gadgets
  74. };
  75.  
  76. //
  77. // class TGadgetWindowFont
  78. // ~~~~~ ~~~~~~~~~~~~~~~~~
  79. // Specify the point size of the font (not the size in pixels) and whether it
  80. // is bold and/or italic. you get a variable width sans serif font(typically
  81. // Helvetica)
  82. //
  83. class _OWLCLASS TGadgetWindowFont : public TFont {
  84.   public:
  85.     TGadgetWindowFont(int  pointSize = 10,
  86.                       bool bold = false,
  87.                       bool italic = false);
  88.     TGadgetWindowFont(const TFont& font);
  89. };
  90.  
  91. //
  92. // class TGadgetWindow
  93. // ~~~~~ ~~~~~~~~~~~~~
  94. // - Maintains a list of tiled Gadgets
  95. // - You specify whether the Gadgets are tiled horizontally or vertically
  96. // - A font that the Gadgets should use
  97. // - You can specify left, right, top, and bottom margins. The Gadgets are
  98. //   situated within the inner rectangle (area excluding borders and
  99. //   margins). Margins can be specified in pixels, layout units (based on
  100. //   the window font), or border units (width or height of a thin window
  101. //   border)
  102. // - You can specify that the Gadget window shrink wrap itself to "fit"
  103. //   around its Gadgets in either the width or height or both (default for
  104. //   horizontally tiled Gadgets is shrink wrap height and default for
  105. //   vertically tiled Gadgets is shrink wrap width)
  106. //
  107. class _OWLCLASS TGadgetWindow : virtual public TWindow, public TGadgetList {
  108.   public:
  109.  
  110.     // Enumeration describing how gadgets should be laid out within the
  111.     // gadget window.
  112.     //
  113.     enum TTileDirection {
  114.       Horizontal,       // Arrange gadgets in a row
  115.       Vertical,         // Arrange gadgets in a column
  116.       Rectangular       // Arrange gadgets in rows & columns [2-D]
  117.     };
  118.  
  119.     TGadgetWindow(TWindow*        parent = 0,
  120.                   TTileDirection  direction = Horizontal,
  121.                   TFont*          font = new TGadgetWindowFont,
  122.                   TModule*        module = 0);
  123.    ~TGadgetWindow();
  124.  
  125.     // Override TWindow member function and choose initial size if shrink
  126.     // wrapping was requested
  127.     //
  128.     bool          Create();
  129.  
  130.     // Changes the margins and initiates a layout session
  131.     //
  132.     void          SetMargins(TMargins& margins);
  133.  
  134.     // Get or set the direction. Setting the direction swaps dimentional
  135.     // members & initiates a layout session
  136.     //
  137.     TTileDirection GetDirection() const;
  138.     virtual void  SetDirection(TTileDirection direction);
  139.  
  140.     void          SetRectangularDimensions(int width, int height, int rowMargin= -1);
  141.  
  142.     TFont&        GetFont();
  143.     uint          GetFontHeight() const;
  144.  
  145.     // Retrieves/assigns tooltip of/to gadgetwindow
  146.     //
  147.     TTooltip*     GetTooltip() const;
  148.     void          SetTooltip(TTooltip* tooltip);
  149.     void          EnableTooltip(bool enable);
  150.  
  151.     // Getting & releasing of the mouse by gadgets.
  152.     //
  153.     bool          GadgetSetCapture(TGadget& gadget);
  154.     void          GadgetReleaseCapture(TGadget& gadget);
  155.  
  156.     // Hint mode settings & action used by contained gadgets
  157.     //
  158.     enum THintMode {
  159.       NoHints, 
  160.       PressHints, 
  161.       EnterHints
  162.     };
  163.     void          SetHintMode(THintMode hintMode);  
  164.     THintMode     GetHintMode();
  165.     void          SetHintCommand(int id);  // (id <= 0) to clear
  166.  
  167.     // TGadgetList list management overrides
  168.     // Insert & remove a Gadget, update their Window ptr to this window
  169.     //
  170.     void     Insert(TGadget& gadget, TPlacement = After, TGadget* sibling = 0);
  171.     void     InsertFrom(TGadgetList& list, TPlacement = After, TGadget* sibling = 0);
  172.     TGadget* Remove(TGadget& gadget);
  173.  
  174.     // Override callbacks invoked when a gadget is removed or inserted
  175.     // Locate and iterate over gadgets
  176.     //
  177.     void     Inserted(TGadget& gadget);
  178.     void     Removed(TGadget& gadget);
  179.  
  180.     // Shared CelArray management
  181.     //
  182.     virtual void          SetCelArray(TCelArray* sharedCels);
  183.     virtual TCelArray&    GetCelArray(int minX = 0, int minY = 0);
  184.  
  185.     // During idle time, iterates over the Gadgets invoking their
  186.     // CommandEnable() member function
  187.     //
  188.     bool          IdleAction(long idleCount);
  189.  
  190.     // Set timer - useful for gadgets that need regular update [Time/Date]
  191.     //
  192.     bool          EnableTimer();
  193.  
  194.     // Sent by a Gadget when its size has changed. Initiates a layout session
  195.     //
  196.     void          GadgetChangedSize(TGadget& gadget);
  197.  
  198.     // Begins a layout session which tiles the Gadgets & repaints
  199.     //
  200.     virtual void  LayoutSession();
  201.  
  202.     // Simply sets the corresponding member data
  203.     //
  204.     void          SetShrinkWrap(bool shrinkWrapWidth, bool shrinkWrapHeight);
  205.  
  206.     // Get the desired size for this gadget window.
  207.     //
  208.     virtual void  GetDesiredSize(TSize& size);
  209.  
  210.   protected:
  211.     // Called by Paint(). Iterates over the Gadgets and asks each one to draw
  212.     //
  213.     virtual void  PaintGadgets(TDC& dc, bool erase, TRect& rect);
  214.  
  215.     // Computes the area inside of the borders and margins
  216.     //
  217.     virtual void  GetInnerRect(TRect& rect);
  218.  
  219.     int           LayoutUnitsToPixels(int units);
  220.  
  221.     void          GetMargins(const TMargins& margins,
  222.                              int& left, int& right, int& top, int& bottom);
  223.     void          UseDesiredSize();
  224.  
  225.     // Gadget layout information used during the layout process
  226.     //
  227.     class TLayoutInfo {
  228.       public:
  229.         TLayoutInfo(int gadgetCount)
  230.             : GadgetBounds(new TRect[gadgetCount]), DesiredSize(0,0) {}
  231.         TSize            DesiredSize;
  232.         TAPointer<TRect> GadgetBounds;
  233.     };
  234.  
  235.     // Calculates the layout of the Gadgets in the specified direction
  236.     // Returns the TLayoutInfo to describe the result but does not move the
  237.     // gadgets.
  238.     //
  239.     virtual void  LayoutGadgets(TTileDirection dir, TLayoutInfo& layout);
  240.  
  241.     // Tiles the Gadgets in the current direction
  242.     //
  243.     virtual TRect TileGadgets();
  244.  
  245.     // Selects the font into the DC and calls PaintGadgets()
  246.     //
  247.     void          Paint(TDC& dc, bool erase, TRect& rect);
  248.  
  249.     virtual void  PositionGadget(TGadget* previous, TGadget* next,
  250.                                  TPoint& point);
  251.  
  252.     // Overrides of TWindow virtuals
  253.     //
  254.     void          SetupWindow();
  255.     void          CleanupWindow();
  256.     bool          PreProcessMsg(MSG& msg);
  257.  
  258.     // Message response functions
  259.     //
  260.     void          EvLButtonDown(uint modKeys, TPoint& point);
  261.     void          EvLButtonUp(uint modKeys, TPoint& point);
  262.     void          EvLButtonDblClk(uint modKeys, TPoint& point);
  263.     void          EvRButtonDown(uint modKeys, TPoint& point);
  264.     void          EvRButtonUp(uint modKeys, TPoint& point);
  265.     void          EvMouseMove(uint modKeys, TPoint& point);
  266.     void          EvWindowPosChanging(WINDOWPOS far& windowPos);
  267.     void          EvSysColorChange();
  268.  
  269.     void          EvCreateTooltips();
  270.  
  271.     // The overridden event handler functions are virtual, and not dispatched
  272.     // thru response tables
  273.     //
  274.     TResult       EvCommand(uint id, HWND hWndCtl, uint notifyCode);
  275.     void          EvCommandEnable(TCommandEnabler& ce);
  276.     TResult       EvNotify(uint id, TNotify far& notifyInfo);
  277.  
  278.   // Protected data members
  279.   //
  280.   protected_data:
  281.     TFont*          Font;           // Font used for size calculations
  282.     TGadget*        Capture;        // Gadget that has captured the mouse
  283.     TGadget*        AtMouse;        // Last Gadget at mouse position
  284.     TMargins        Margins;
  285.     uint            FontHeight       : 8;
  286.     bool            ShrinkWrapWidth  : 8;
  287.     bool            ShrinkWrapHeight : 8;
  288.     uint            WideAsPossible   : 8;  // # of "WideAsPossible" gadgets
  289.     bool            DirtyLayout      : 8;
  290.     TTileDirection  Direction        : 8;
  291.     THintMode       HintMode         : 8;
  292.     TCelArray*      SharedCels;     // CelArray that can be shared by gadgets
  293.     TTooltip*       Tooltip;        // Gadget window's tooltip
  294.     bool            WantTooltip;    // Flags whether to create a tooltip
  295.     int             RowWidth;       // Requested max width of each row
  296.     int             RowMargin;      // Settable inter-row margin
  297.     bool            WantTimer;      // Flags whether to start a timer
  298.     uint            TimerID;        // Timer identifier
  299.  
  300.   private:
  301.     // Calculate layouts metrics for window and all gadgets for a given
  302.     // direction
  303.     //
  304.     void          LayoutHorizontally(TLayoutInfo&);
  305.     void          LayoutVertically(TLayoutInfo&);
  306.     void          LayoutRectangularly(TLayoutInfo&);
  307.     void          FinishRow(int, TGadget*, TGadget*, int, TLayoutInfo&, int&);
  308.  
  309.     // Hidden to prevent accidental copying or assignment
  310.     //
  311.     TGadgetWindow(const TGadgetWindow&);
  312.     TGadgetWindow& operator =(const TGadgetWindow&);
  313.  
  314.   friend class TGadget;
  315.  
  316.   DECLARE_RESPONSE_TABLE(TGadgetWindow);
  317.   DECLARE_CASTABLE;
  318. };
  319.  
  320.  
  321. #define EV_WM_CREATETOOLTIP\
  322.   {WM_OWLCREATETTIP, 0, (TAnyDispatcher)::v_Dispatch,\
  323.    (TMyPMF)v_Sig(&TMyClass::EvCreateTooltips)} 
  324.  
  325.  
  326. //
  327. // class TGadgetControl
  328. // ~~~~~ ~~~~~~~~~~~~~~
  329. // Specialized gadget window that is easy to construct holding one gadget for
  330. // use as a control in a window.
  331. //
  332. class _OWLCLASS TGadgetControl : public TGadgetWindow {
  333.   public:
  334.     TGadgetControl(TWindow*        parent = 0,
  335.                    TGadget*        soleGadget = 0,
  336.                    TFont*          font = new TGadgetWindowFont,
  337.                    TModule*        module = 0);
  338. };
  339.  
  340. // Generic definitions/compiler options (eg. alignment) following the
  341. // definition of classes
  342. #include <services/posclass.h>
  343.  
  344. #if defined(BI_NAMESPACE)
  345. } // namespace OWL
  346. #endif
  347.  
  348. //----------------------------------------------------------------------------
  349. // Inline implementations
  350.  
  351. //
  352. // Return the number of gadgets in the list.
  353. //
  354. inline uint TGadgetList::GadgetCount() const
  355. {
  356.   return NumGadgets;
  357. }
  358.  
  359. //
  360. // Return the first gadget in the list.
  361. //
  362. inline TGadget* TGadgetList::FirstGadget() const
  363. {
  364.   return Gadgets;
  365. }
  366.  
  367. //
  368. // Return the next gadget in the list relative to a given gadget
  369. //
  370. inline TGadget* TGadgetList::NextGadget(TGadget& gadget) const
  371. {
  372.   return gadget.NextGadget();
  373. }
  374.  
  375. //----------------------------------------------------------------------------
  376.  
  377. //
  378. // Return the direction of tiling for this gadget window.
  379. //
  380. inline TGadgetWindow::TTileDirection TGadgetWindow::GetDirection() const
  381. {
  382.   return Direction;
  383. }
  384.  
  385. //
  386. // Return the font being used by this gadget window.
  387. //
  388. inline TFont& TGadgetWindow::GetFont()
  389. {
  390.   return *Font;
  391. }
  392.  
  393. //
  394. // Return the height of the font being used by this gadget window.
  395. //
  396. inline uint TGadgetWindow::GetFontHeight() const
  397. {
  398.   return FontHeight;
  399. }
  400.  
  401. //
  402. // Set the hint mode of this gadget window to one of the THintMode enum values
  403. //
  404. inline void TGadgetWindow::SetHintMode(THintMode hintMode)
  405. {
  406.   HintMode = hintMode;
  407. }
  408.  
  409. //
  410. // Return the current hint mode of this gadget window.
  411. //
  412. inline TGadgetWindow::THintMode TGadgetWindow::GetHintMode()
  413. {
  414.   return HintMode;
  415. }
  416.  
  417. //
  418. // Retrieve the sizes of the 4 margins in pixels for this gadget window given a
  419. // margin object
  420. //
  421. inline void
  422. TGadgetWindow::GetMargins(const TMargins& margins,
  423.                           int& left, int& right, int& top, int& bottom)
  424. {
  425.   margins.GetPixels(left, right, top, bottom, FontHeight);
  426. }
  427.  
  428. #endif  // OWL_GADGETWI_H
  429.